package com.imy.luascript.app.ui.main.fragment.project;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.common.base.adapter.BaseDelegate;
import com.imy.common.base.adapter.BaseRecyclerViewHolder;
import com.imy.common.util.ALog;
import com.imy.common.util.DateUtils;
import com.imy.common.util.FileUtils;
import com.imy.common.util.ImageUtils;
import com.imy.common.util.ToastUtil;
import com.imy.lua.dialog.FileNameDialog;
import com.imy.lua.helper.properties.PropertiesLoaderHelper;
import com.imy.luascript.R;
import com.imy.luascript.app.data.general.ProjectItem;
import com.imy.luascript.app.ui.dialog.FileSettingDialog;
import com.imy.luascript.event.ProjectEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.Properties;

import butterknife.BindView;

public class ProjectDelegate extends BaseDelegate<ProjectItem, ProjectDelegate.Holder> {


    public ProjectDelegate(Activity activity) {
        super(activity);
    }

    @Override
    protected boolean isShow(ProjectItem s) {
        return s.type == ProjectItem.TYPE_FILE;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.recycler_item_project;
    }

    @Override
    protected Holder getHolder(View view) {
        return new Holder(view);
    }

    @Override
    protected void bindView(Holder holder, int position, ProjectItem s) {
        if (position == 0) {
            holder.lineView.setVisibility(View.GONE);
        } else {
            holder.lineView.setVisibility(View.VISIBLE);
        }

        File file = s.getFile();
        boolean readSize = true;
        holder.itemProjectDebugIv.setVisibility(View.GONE);
        if (file.isDirectory()) {
            holder.itemProjectIv.setImageResource(R.mipmap.icon_dir);
            readSize = false;
        } else if (file.getAbsolutePath().endsWith(".lua")) {
            holder.itemProjectIv.setImageResource(R.mipmap.icon_lua);
        } else if (file.getName().equals("global.properties")) {
            holder.itemProjectIv.setImageResource(R.mipmap.icon_config);
            holder.itemProjectDebugIv.setVisibility(View.VISIBLE);
        } else if (file.getName().endsWith(".png")
                || file.getName().endsWith(".jpg")) {
            Bitmap imageThumbnail = ImageUtils.getImageThumbnail(file.getAbsolutePath(), 60, 60);
            holder.itemProjectIv.setImageBitmap(imageThumbnail);
        } else {
            holder.itemProjectIv.setImageResource(R.mipmap.icon_un_know_file);
        }
        if (readSize) {
            String size = "文件大小:" + FileUtils.getFileSizeString(file);
            holder.fileSizeTv.setText(size);
            holder.fileSizeTv.setVisibility(View.VISIBLE);
        } else {
            holder.fileSizeTv.setVisibility(View.GONE);
        }
        holder.itemProjectName.setText(file.getName());
        long l = file.lastModified();
        holder.lastEditTv.setText("修改于：" + DateUtils.format(l));
        holder.itemProjectMoreIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onMoreClicked(s);
            }
        });
        holder.itemProjectDebugIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置调试工程
                File file = s.file;
                Properties properties = PropertiesLoaderHelper.loadGlobal(appCompatActivity, file.getAbsolutePath());
                if (properties != null) {
                    ToastUtil.toast(appCompatActivity, "设置为调试工程成功");
                }
            }
        });
    }

    private void onMoreClicked(ProjectItem s) {
        FileSettingDialog fileSettingDialog = new FileSettingDialog((AppCompatActivity) appCompatActivity);
        fileSettingDialog.setFileSettingChangeListener(new FileSettingDialog.FileSettingChangeListener() {
            @Override
            public void onDelete() {
                FileUtils.deleteDir(s.file);
                EventBus.getDefault().post(ProjectEvent.create(ProjectEvent.REFRESH));
            }

            @Override
            public void onRename() {
                FileNameDialog fileNameDialog = new FileNameDialog((AppCompatActivity) appCompatActivity);
                fileNameDialog.show("请重新命名");
                fileNameDialog.getFileNameEt().setHint("请输入命名");
                fileNameDialog.setFileListener(fileName -> {
                    syncConfigFile(s.file, fileName);
                    FileUtils.reNameFile(s.file, fileName);
                    //判断是否有配置文件 有配置文件需要同步
                    EventBus.getDefault().post(ProjectEvent.create(ProjectEvent.REFRESH));
                });
            }

            @Override
            public void onSetCurrentProject() {

            }
        });
    }

    private void syncConfigFile(File file, String fileName) {
        File configFile = getConfigFile(file);

        if (configFile != null) {
            //读取
            String fileSizeString = FileUtils.getStringForFile(configFile);
            ALog.i("原来的配置文件:" + fileSizeString);
            String s = fileSizeString.replaceAll("/" + file.getName(), "/" + fileName);
            configFile.delete();
            FileUtils.saveStringToFile(s, file.getAbsolutePath() + "/global.properties");
        }
    }

    private File getConfigFile(File file) {
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            return null;
        }
        for (int i = 0; i < files.length; i++) {
            File file1 = files[i];
            if (file1.getName().equals("global.properties")) {
                return file1;
            }
        }
        return null;
    }


    public static class Holder extends BaseRecyclerViewHolder {
        @BindView(R.id.line_view)
        View lineView;
        @BindView(R.id.item_project_iv)
        ImageView itemProjectIv;
        @BindView(R.id.item_project_name)
        TextView itemProjectName;
        @BindView(R.id.file_size_tv)
        TextView fileSizeTv;
        @BindView(R.id.last_edit_tv)
        TextView lastEditTv;
        @BindView(R.id.item_project_more_iv)
        ImageView itemProjectMoreIv;
        @BindView(R.id.item_project_debug_iv)
        ImageView itemProjectDebugIv;

        public Holder(View itemView) {
            super(itemView);
        }
    }


}
